package game.UIComponent
{
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import game.common.CustomBitmap;
	import game.controller.GameDispatcher;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.LanguageManager;
	import game.utils.Utils;

	public class FoodTypeSquare extends UIBase
	{
		private var _bg:CustomBitmap;
		private var _foodType:CustomBitmap;
		private var _source:String;

		private var _nameTF:TextField;

		private var _costTF:TextField;
		private var _costNumTF:TextField;

		private var _priceTF:TextField;
		private var _priceNumTF:TextField;

		private var _exTF:TextField;
		private var _exNumTF:TextField;

		private var _buyBtn:Button;

		private var _name:String;
		private var _cost:Number;
		private var _price:Number;
		private var _ex:Number;

		public function FoodTypeSquare(type:String, name:String, cost:Number, price:Number, ex:Number)
		{
			_name=name;
			_cost=cost;
			_price=price;
			_ex=ex;
			_source=type;

			_bg=new CustomBitmap();
			_bg.source="foodBg";
			addChild(_bg);

			_foodType=new CustomBitmap();
			_foodType.buttonMode=true;
			_foodType.source=_source;
			_foodType.x=5;
			_foodType.y=5;
			_foodType.addEventListener(MouseEvent.CLICK, onClick);
			addChild(_foodType);

			init();
			
			
		}

		override protected function init():void
		{
			_nameTF=Utils.getCommonTextField(_name, 15);
			_nameTF.x=_foodType.x + _foodType.width + 3;
			_nameTF.y=_foodType.y;
			addChild(_nameTF);

			_costTF=Utils.getCommonTextField(LanguageManager.getWord("menu.cost"), 12);
			_costTF.x=_nameTF.x;
			_costTF.y=_nameTF.y + _nameTF.height - 3;
			addChild(_costTF);

			_costNumTF=Utils.getTextField(_cost.toString(), 12);
			_costNumTF.x=_costTF.x + _costTF.width + 3;
			_costNumTF.y=_costTF.y;
			addChild(_costNumTF);

			_priceTF=Utils.getCommonTextField(LanguageManager.getWord("menu.price"), 12);
			_priceTF.x=_nameTF.x;
			_priceTF.y=_costTF.y + _costTF.height - 3;
			addChild(_priceTF);

			_priceNumTF=Utils.getTextField(_price.toString(), 12);
			_priceNumTF.x=_priceTF.x + _priceTF.width + 3;
			_priceNumTF.y=_priceTF.y;
			addChild(_priceNumTF);

			_exTF=Utils.getCommonTextField(LanguageManager.getWord("menu.ex"), 12);
			_exTF.x=_nameTF.x;
			_exTF.y=_priceTF.y + _priceTF.height - 3;
			addChild(_exTF);

			_exNumTF=Utils.getTextField(_ex.toString(), 12);
			_exNumTF.x=_exTF.x + _exTF.width + 3;
			_exNumTF.y=_exTF.y;
			addChild(_exNumTF);

			_buyBtn=new Button("", "miniBuyBtn");
			_buyBtn.x=_exTF.x;
			_buyBtn.y=_exTF.y + _exTF.height;
			_buyBtn.addEventListener(MouseEvent.CLICK, onClick);
			addChild(_buyBtn);
		}

		private var data:Object;

		private function onClick(e:MouseEvent):void
		{
			if (Global.foodFull == false)
			{
				data=new Object();
				data.name=_name;
				data.cost=_cost;
				data.price=_price;
				data.ex=_ex;
				data.source=_source;				
				GameDispatcher.getInstance().dispatchEvent(new GameEvent(EventName.OPEN_SELLPANEL, data));
			}
			if (Global.foodFull && Global.foodArr.indexOf(_source) != -1)
			{
				data=new Object();
				data.name=_name;
				data.cost=_cost;
				data.price=_price;
				data.ex=_ex;
				data.source=_source;
				//Global.foodArr.push(_source);
				GameDispatcher.getInstance().dispatchEvent(new GameEvent(EventName.OPEN_SELLPANEL, data));
			}
		}

		public function get source():String
		{
			return _source;
		}

		public function set source(value:String):void
		{
			_source = value;
		}

		
		/**
		 * 检查自身的source是否和已选定的3个source吻合,如吻合则为true,不吻合则为false
		 * */
		public function checkBuyBtn():void
		{
			if (Global.foodFull && Global.foodArr.indexOf(_source) == -1)
			{
				_buyBtn.enabled = false;
			}else
				_buyBtn.enabled = true;
			
		}


	}
}